function [EC] = fakeSERDS(filename,setoff,settype,setplot,vargin)
%% FAKESERDS(filename,setoff,settype,setplot)  --  Make fake SERDS dataset
% Takes data from the parameterized file and shifts the ALS corrected data
% before readding the background. Saves both datasets as examples for SERDS
% analysis
%
% INPUT:    filename    : filename of dataset to be used as template
%           setoff      : amount of pixels to shift
%           settype     : 0=[offset in Pixel], 1=[offset in wavelength (nm)]
%           setplot     : set to 0 to avoid plot
%
% OUTPUT:   EC
%

%  (c) F. Dworkowski, 2012
%      Version 1.1, 2012-07-09
%      Version 1.0, 2012-06-29

%% CONFIG
if nargin < 1 || nargin > 4
    help fakeSERDS
    EC = 1; % exit with EC indicating ERROR
    return
elseif nargin < 2
    setoff      = 25; %setoff of SERDS lasers in pixel
    settype     = 0;
    setplot     = 1;
elseif nargin < 3
    settype     = 0;
    setplot     = 1;
elseif nargin < 4
    setplot     = 1;
end

%% CODE
load(filename);
if ~exist('ALSdatay','var')
    error('ERROR: Dataset must contain ALS corrected Data!');
    return
end

lengthdatax = length(datax);

%% Make dataset1.
switch settype
    case 0
        dataset1.datax = datax(1:lengthdatax-setoff);
        dataset1.datay = datay(1:lengthdatax-setoff);
    case 1
        idx1=find(datax>(min(datax)+setoff),1,'first');
        dataset1.datax = datax(1:lengthdatax-idx1);
        dataset1.datay = datay(1:lengthdatax-idx1);
end
dataset1.footer = footer;

%% Make dataset2.
switch settype
    case 0
        dataset2.datax = datax(1:lengthdatax-setoff);
        dataset2.datay = ALSdatay((setoff+1):lengthdatax)+ALSbasey(1:lengthdatax-setoff);
    case 1
        dataset2.datax = datax(1:lengthdatax-idx1);
        dataset2.datay = ALSdatay((idx1+1):lengthdatax)+ALSbasey(1:lengthdatax-idx1);
end
dataset2.footer = footer;

%% Make SERDS dataset
SERDSdata.datax = dataset1.datax;
SERDSdata.datay = dataset2.datay-dataset1.datay;
SERDSdata.setoff = setoff;
switch settype
    case 0
        SERDSdata.setofftype = 'Pixels';
    case 1
        SERDSdata.setofftype = 'Wavelength';
end

%% plot data for debug
if setplot == 1
    figure(77)
        subplot(2,1,1)
            plot(dataset1.datax, dataset1.datay,'r')
            hold on
            plot(dataset2.datax, dataset2.datay,'b')
            hold off
        subplot(2,1,2)
            plot(SERDSdata.datax, SERDSdata.datay,'k')
end
        
%% save new Datasets

save([filename,'_SERDS1'],'-struct','dataset1');
    disp([filename,'_SERDS1.mat saved.']);
save([filename,'_SERDS2'],'-struct','dataset2');
    disp([filename,'_SERDS2.mat saved.']);
save(['SERDS_',filename],'dataset1','dataset2','SERDSdata');
    disp(['SERDS_',filename,'.mat saved.']);
    

EC = 0; % exit with EC indicating OK
        
        